package org.alvin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ZoneParser {

    public static void main(String[] args) throws IOException {
        String url = "https://www.mca.gov.cn/mzsj/xzqh/2022/202201xzqh.html";
        Document document = HttpUtil.get(url);
        Element table = document.getElementsByTag("table").get(0);
        Elements trs = table.getElementsByTag("tr");
        JSONArray resultArray = new JSONArray();
        Map<String, JSONObject> treeMap = Maps.newHashMap();
        String currentShengCode = null;
        String currentShiCode = null;
        List<String> zhixiaoshi = Lists.newArrayList("110000", "120000", "310000", "500000");
        for (int i = 3; i < trs.size(); i++) {
            int type = 1;
            JSONObject jsonObject = new JSONObject();
            Element tr = trs.get(i);
            Elements tds = tr.children();
            Element codeEl = tds.get(1);
            Element nameEl = tds.get(2);
            Elements spanEls = nameEl.getElementsByTag("span");
            if (spanEls == null || spanEls.isEmpty()) {
                type = 1;
                jsonObject.put("children", new JSONArray());
                jsonObject.put("pcode", "0");
                currentShengCode = codeEl.text().trim();
                resultArray.add(jsonObject);
            } else {
                Element spanEl = spanEls.get(0);
                String text = spanEl.html();
                if (text.trim().contains("&nbsp;&nbsp;")) {
                    type = 3;
                    if (zhixiaoshi.indexOf(currentShengCode) != -1) {
                        jsonObject.put("pcode", currentShengCode);
                        treeMap.get(currentShengCode).getJSONArray("children").add(jsonObject);
                    } else {
                        jsonObject.put("pcode", currentShiCode);
                        treeMap.get(currentShiCode).getJSONArray("children").add(jsonObject);
                    }
                } else if (text.trim().contains("&nbsp;")) {
                    type = 2;
                    jsonObject.put("children", new JSONArray());
                    jsonObject.put("pcode", currentShengCode);
                    treeMap.get(currentShengCode).getJSONArray("children").add(jsonObject);
                    currentShiCode = codeEl.text().trim();
                }
            }
            jsonObject.put("type", type);
            jsonObject.put("value", codeEl.text().trim());
            jsonObject.put("label", nameEl.text().trim());
            treeMap.put(jsonObject.getString("value"), jsonObject);
            if (jsonObject.getString("label").trim().equals("澳门特别行政区")) {
                break;
            }
        }
//        //tree结构转列表算法
        List<JSONObject> resultList = new ArrayList<>();
        LinkedList<JSONObject> linkedList = new LinkedList<>();
        int id = 0;
        for (int i = 0; i < resultArray.size(); i++) {
            linkedList.add(resultArray.getJSONObject(i));
        }
        while (!linkedList.isEmpty()) {
            JSONObject item = linkedList.removeFirst();
//            id ++;
//            item.put("id",id);
            item.remove("type");
            item.remove("pcode");
            resultList.add(item);
            //广度优先算法
            if (item.containsKey("children")) {
                JSONArray jsonArray = item.getJSONArray("children");
                for (int i = 0; i < jsonArray.size(); i++) {
                    linkedList.add(jsonArray.getJSONObject(i));
                }
//                item.remove("children");
            }else {
                item.put("children",new JSONArray());
            }
        }
        System.out.println(resultArray);
//        for (JSONObject jsonObject : resultList) {
//            Integer pid = 0;
//            if(jsonObject.getIntValue("type") == 1){
//                pid = 0;
//            }else {
//                pid = treeMap.get(jsonObject.getString("pcode")).getIntValue("id");
//            }
//            String sql = "INSERT INTO `xl_base_zone` (`id`, `parent_id`, `zone_name`, `zone_code`) VALUES ("+jsonObject.getIntValue("id")+", "+pid+", '"+jsonObject.getString("label")+"', "+jsonObject.getString("value")+");";
//            System.out.println(sql);
//        }
    }
}
